<?php
/**
 * Created by PhpStorm.
 * User: s.utkin
 * Date: 15.05.2015
 * Time: 20:31
 */

namespace SitemapGenerator\DataProvider;


use SitemapGenerator\FinderIterator;
use Traversable;

/**
 * Class ModelField
 * @package SitemapGenerator\Dataprovider
 */
class ModelFieldProvider implements \IteratorAggregate {

	/**
	 * @var \Pinenut\Model2\Finder
	 */
	protected $finder = null;
	protected $field = null;

	/**
	 * @param $params
	 */
	public function __construct($params)
	{
		$modelClass = $params['model'];
		$this->field = isset($params['field']) ? $params['field'] : null;

		if (!method_exists($modelClass, 'find')) {
			\Log::error(__METHOD__, "Class '{$modelClass}' must return Finder");
			return;
		}

		$this->finder = $modelClass::find();
	}


	/**
	 * (PHP 5 &gt;= 5.0.0)<br/>
	 * Retrieve an external iterator
	 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
	 * @return Traversable An instance of an object implementing <b>Iterator</b> or
	 * <b>Traversable</b>
	 */
	public function getIterator()
	{
		return new FinderIterator($this->finder, $this->field, 1000);
	}
}